/*
 * Copyright 2014-2017 ieclipse.cn.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.github.biezhi.wechat.handler.msg;

import org.dom4j.Element;

import io.github.biezhi.wechat.model.WechatMessage;
import io.github.biezhi.wechat.model.xml.AppMsgInfo;

/**
 * msg type = 3
 * 
 * @author Jamling
 * @date 2017年12月22日
 *       
 */
public class ImageMsgXmlHandler extends AbstractMsgXmlHandler {
    
    public ImageMsgXmlHandler() {
        super();
    }
    
    public ImageMsgXmlHandler(WechatMessage m) {
        super(m);
    }
    
    @Override
    public boolean isParseXmlContent() {
        return false;
    }
    
    @Override
    public boolean isUnescape() {
        return false;
    }
    
    public String getHtml(String link) {
        try {
            if (link == null) {
                return "对方给您发了一张图片（请在手机上查看）";
            }
            if (link != null) {
                AppMsgInfo info = new AppMsgInfo();
                if (message != null) {
                    message.AppMsgInfo = info;
                }
                info.url = link;
                String img = String.format(
                        "<img src=\"%s\" width=\"%s\" height=\"%s\" alt=\"图片\"/>",
                        link, message.ImgWidth, message.ImgHeight);
                String html = String.format(
                        "<a href=\"%s\" title=\"点击查看大图\">%s</a>", link, img);
                return html;
            }
            
            AppMsgInfo info = new AppMsgInfo();
            message.AppMsgInfo = info;
            Element node = root.element("img");
            String q = getQueryString();
            if (q == null) {
                q = "";
            }
            String w = node.attributeValue("cdnthumbwidth");
            String h = node.attributeValue("cdnthumbheight");
            String url = node.attributeValue("cdnthumburl");
            info.url = url;
            String img = String.format(
                    "<img src=\"%s\" width=\"%s\" height=\"%s\" alt=\"图片\"/>",
                    link, w, h);
            String html = String
                    .format("<a href=\"%s\" title=\"点击查看大图\">%s</a>", url, img);
            return html;
        } catch (Exception e) {
            return "图片消息，请在手机上查看";
        }
    }
}
